﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;

namespace VodafoneWP7
{
    public class SavedOptions
    {
        public bool ConfirmOnExit;

        public SavedOptions()
        {
            ConfirmOnExit = true;
        }
    }

    public static class Storage
    {

        public static List<Account> LoadAccounts()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var streamFile = appStorage.OpenFile("accounts.xml", FileMode.OpenOrCreate))
                {
                    try
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Account>));
                        using (var reader = new StreamReader(streamFile))
                        {
                            //var a = serializer.Deserialize(reader) as List<Account>;
                            //for (int i = 0; i < a.Count; i++)
                            //    a[i].messaggingService = MessaggingServices.GetServices(a[i].serviceType);
                            return serializer.Deserialize(reader) as List<Account>;
                        }
                    }
                    catch (Exception ex) {                         
                        ex = ex;
                        return new List<Account>();

                    }
                }
            }
        }

        public static void DeleteAccounts()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("accounts.xml");
            }
        }

        public static void SaveAccounts(List<Account> accounts)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("accounts.xml");
                using (var streamFile = appStorage.OpenFile("accounts.xml", FileMode.OpenOrCreate))
                using (var writer = new StreamWriter(streamFile))
                //write_options_state(writer, entry);
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Account>));
                    serializer.Serialize(writer, accounts);
                }
            }
        }

        public static SavedOptions LoadSavedOptions()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var streamFile = appStorage.OpenFile("SavedOptions.xml", FileMode.OpenOrCreate))
                {
                    try
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(SavedOptions));
                        using (var reader = new StreamReader(streamFile))
                            return serializer.Deserialize(reader) as SavedOptions;
                    }
                    catch (Exception) { return new SavedOptions(); }
                }
            }
        }

        public static void DeleteSavedOptions()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("SavedOptions.xml");
            }
        }

        public static void SaveSavedOptions(SavedOptions state)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("SavedOptions.xml");
                using (var streamFile = appStorage.OpenFile("SavedOptions.xml", FileMode.OpenOrCreate))
                using (var writer = new StreamWriter(streamFile))
                //write_options_state(writer, entry);
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SavedOptions));
                    serializer.Serialize(writer, state);
                }
            }
        }

    }
}
